package com.itheima.train.manager.utils;

import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class BeanMapConverter {

    // 把一个对象转换成Map
    public Map<String  , String> beanToMap(Object obj) {

        // 创建一个Map对象
        Map<String , String> resultMap = new HashMap<>() ;
        BeanMap beanMap = BeanMap.create(obj);
        beanMap.forEach( (key , value) -> {
            resultMap.put(key.toString() , value == null ? "" : value.toString()) ;
        });

        // 返回
        return resultMap ;

    }

}
